package me.pplay.playtalk.adapter;

import java.util.List;
import java.util.Map;

import me.pplay.playtalk.bean.CommonControlID;
import me.pplay.playtalk.callback.CommonAdapterOnClickCallBack;
import me.pplay.playtalk.holder.CommonHolder;
import me.pplay.playtalk.util.LogManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommonAdapter extends BaseAdapter {
	private CommonHolder holder;
	private CommonControlID holderId;
	private Context mContext;
	private LayoutInflater mInflater;
	private List<Map<String, Object>> mData;
	private CommonAdapterOnClickCallBack onClick;

	public CommonAdapter(Context context, List<Map<String, Object>> list, CommonControlID holderId, CommonAdapterOnClickCallBack onClick) {
		this.mContext = context;
		this.mData = list;
		this.mInflater = LayoutInflater.from(this.mContext);
		this.onClick = onClick;
		this.holderId = holderId;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if (arg1 == null) {

			holder = new CommonHolder();
			arg1 = mInflater.inflate(holderId.itemLayout, null);
			// arg1 = mInflater.inflate(R.layout.layout_communitylist, null);
			if (holderId.leftImgId != 0)
				holder.leftImg = (ImageView) arg1.findViewById(holderId.leftImgId);
			holder.leftStr = (TextView) arg1.findViewById(holderId.leftStrId);
			holder.leftStr1 = (TextView) arg1.findViewById(holderId.leftStr1Id);
			holder.rightStr = (TextView) arg1.findViewById(holderId.rightStrId);
			holder.rightStr1 = (TextView) arg1.findViewById(holderId.rightStr1Id);
			if (holderId.rightImgId != 0)
				holder.rightImg = (ImageView) arg1.findViewById(holderId.rightImgId);
			arg1.setTag(holder);

		} else {
			holder = (CommonHolder) arg1.getTag();
		}
		LogManager.LogShow("CommonAdapter", "holder.rightImg" + holder.rightImg);
		if (holder.leftImg != null) {
			if (mData.get(position).get("leftImg") != null)
				holder.leftImg.setBackgroundResource((Integer) mData.get(position).get("leftImg"));
			else if (mData.get(position).get("leftImgDrwable") != null)
				holder.leftImg.setBackgroundDrawable((Drawable)mData.get(position).get("leftImgDrwable"));
			else
				holder.leftImg.setVisibility(View.INVISIBLE);

		}

		if (holder.rightImg != null) {
			if (mData.get(position).get("rightImg") != null)
				holder.rightImg.setBackgroundResource((Integer) mData.get(position).get("rightImg"));
			else
				holder.rightImg.setVisibility(View.INVISIBLE);
		}

		if (holder.leftStr != null && mData.get(position).get("leftStr") != null)
			holder.leftStr.setText((String) mData.get(position).get("leftStr"));
		if (holder.leftStr1 != null && mData.get(position).get("leftStr1") != null)
			holder.leftStr1.setText((String) mData.get(position).get("leftStr1"));
		if (holder.rightStr != null && mData.get(position).get("rightStr") != null)
			holder.rightStr.setText((String) mData.get(position).get("rightStr"));
		if (holder.rightStr1 != null && mData.get(position).get("rightStr1") != null)
			holder.rightStr1.setText((String) mData.get(position).get("rightStr1"));
		if (holder.rightImg != null)
			holder.rightImg.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// goto to chatroom
					onClick.OnClickCallback(position, holderId.rightImgId, v);
				}
			});
		return arg1;
	}

}
